<template>
    <div class="md-deliveryTask" v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)">
      <TableTitle style="margin-bottom:8px">
        <slot>
          <p>
            扫码操作
          </p>
        </slot>
      </TableTitle>
      <TopSearch ref="topSm">
        <slot>
          <li><el-button size="mini" type="primary" @click="palletInCar">托盘装车</el-button></li>
          <li><el-button size="mini" type="primary" @click="deliveryBtn">配送</el-button></li>
          <li style="margin-top:-15px">
            <p>小车编码：{{carTM}}</p>
            <p style="margin-top:-5px">工作器具包：<span v-for="(item,index) in gzbqjTM" :key=index>{{item}}，</span></p>
          </li>
        </slot>
      </TopSearch>
      <TopData></TopData>
      <TableTitle>
        <p>
          待配送工作包任务列表
        </p>
      </TableTitle>
      <TopTable></TopTable>
    </div>
  </template>
  <script>
  import TopSearch from "../../../../../components/TopSearch.vue";
  import TableTitle from "../../../../../components/TableTitle.vue";
  import TopData from "./TopShowdata.vue"
  import TopTable from "./TopTable.vue"
  import { mdDtDlivery,workBoxSmOut,mdDtTopMidData } from '@/utils/api.js'
  import request from "@/utils/request.js";
  export default {
    name: "DeliverySendTask",
    components: {TopSearch,TopData, TopTable,TableTitle},
    data(){
      return{
        // 暂时存储Mid表查询所需信息
        topToMIdInfo:'',
        // 存储MidTable传来的信息给配送按钮
        midTableSendInfo:'',
        indexTm:'',
        carTM:'',
        mdDtSendInfo:'',
        gzbCome:[],
        gzbTm:'',
        sendNumber:0,
        scanSendInfo:'',
        resDataInfo:[],
        gzbqjTM:[],
        loading:false,
        workPackageTaskInfo:[],
        calledTools:[]
      }
    },
    methods:{
      // 扫码统一方法
      scanning(){
        if(this.indexTm.indexOf("L-D-")>-1){//小车码
          // 先扫车条码
            this.carTM = this.indexTm
            this.sendNumber = 2
            // 扫码框清空 并 聚焦
            this.$refs.topSm.makeTmNull()
            this.$refs.topSm.openScan()
        }else if(this.indexTm.length == 12) {
          if(this.gzbqjTM.length == 0){
            this.gzbqjTM.push(this.indexTm)
            // 将器具码传给showdata展示
            this.$bus.$emit('mdDtShowGzbqjtm',this.gzbqjTM)
            console.log(12312312)
            // 清空条码输入框
            this.$refs.topSm.makeTmNull()
          }else {
            let iNum = 0
            for(let i=0;i<this.gzbqjTM.length;i++){
              if(this.indexTm != this.gzbqjTM[i]){
                iNum++
                if(iNum == this.gzbqjTM.length){
                  this.gzbqjTM.push(this.indexTm)
                  // 将器具码传给showdata展示
                  this.$bus.$emit('mdDtShowGzbqjtm',this.gzbqjTM)
                  // 清空条码输入框
                  this.$refs.topSm.makeTmNull()
                  break
                }
              }else{
                // 重复扫码
                this.$Alert({
                  title: "失败",
                  message: this.indexTm + '已扫码！',
                  type: "error",
                });
                // 清空条码输入框
                this.$refs.topSm.makeTmNull()
              }
            }
          }
          
        }else if(this.indexTm == 9005){
          // 再扫工作包条码 调配送接口
          this.loading=true;
          this.$options.methods.deliveryBtn.bind(this)()
        }else {
          this.$Alert({
            title: "失败",
            message: '条码有误！',
            type: "error",
          });
          // 清空条码输入框
          this.$refs.topSm.makeTmNull()
        }
      },
      // 下表数据
      lastTable(){
        mdDtTopMidData({
              aoBarcode:this.indexTm,
              pageParameter:{
                rows:20,
                page:1
              }
            }).then((res)=>{
              console.log('下表Data',res)
              if(res.data.retCode == 200){
                // 扫码阶段 获取配送所需信息字段
                this.scanSendInfo = res.data.responseBody.result
              }else {
                this.$Alert({
                  title: "失败",
                  message: res.data.message,
                  type: "error",
                });
              }
            })
      },
      palletInCar(){//托盘装车
        this.loading = true;
        request.post("/MsDeliveryPackageInfoRest/palletInCar",{carCode:this.carTM,toolCodes:this.gzbqjTM}).then((res)=>{
          if(res.data.retCode==200){
            this.$Alert({type:"success",message:res.data.message,title:"成功"});
          }else{
            this.$Alert({type:"error",message:res.data.message,title:"失败"});
          }
          this.loading = false;
          //this.carTM = "";
          this.gzbqjTM = [];
        });
  
      },
      // 配送
      deliveryBtn(){
        this.loading = true;
        mdDtDlivery({
          carCode:this.carTM,
          toolCodes:this.gzbqjTM
        }).then((res)=>{
          console.log('配送',res)
          this.loading = false;
          if(res.data.retCode == 200) {
            this.$Alert({title: "成功", message: '配送成功', type: "success"});
            // this.sendNumber = 0
            // 清空下表
            this.$bus.$emit('mdDtMidDataFront','')
            // 刷新上表
            this.$bus.$emit('mdDtPsSx','刷新')
          }else {
            this.$Alert({title: "失败", message: res.data.message,type: "error"});
          }
          this.loading=false
          // 清空小车编码
          this.carTM = ''
          // 清空器具包条码list
          this.gzbqjTM = []
        });
        var that = this;
        var timer = setTimeout(function(){
          console.log("定时任务执行");
          that.loading = false;
          that.carTM = "";
          that.gzbqjTM = [];
          clearTimeout(timer);
        },5000);
      }
    },
    mounted(){
      //
      this.$bus.$on('sendWorkPackageTaskData', (e) => {
        this.workPackageTaskInfo = e 
      })
      // 接收MidTable 传来数据的msAoMatchInfoId，toolCode给 配送按钮
      this.$bus.$on('mdDtMidToSend',(e)=>{
        this.midTableSendInfo = e
      })
      //获取car条码
      this.$bus.$on('saoma',(e)=>{
        this.indexTm = e
      }) 
      // 再将该数据的msTaskPackageId 传给配送按钮
      this.$bus.$on('mdDtSendInfos',(e)=>{
        this.mdDtSendInfo = e.msTaskPackageId
        this.gzbCome = []
        this.gzbCome.push(e.msTaskAoId)
        // for(let p=0;p<e.length;p++) {
        //   this.gzbCome.push(e[p].msTaskAoId)
        // }
        console.log(e.msTaskAoId,this.gzbCome)
      })
    }
  };
  </script>
  <style scoped>
  .el-button--info {
    background: #29bacd;
    border-color: #29bacd;
  }
  .el-button--info:hover {
    background: #1f9eaf;
    border-color: #1f9eaf;
  }
  
  </style>